#ifndef REGISTER_H
#define REGISTER_H

#include "globals.h"
#include <QMutex>


class AData;
class DataMidi;
class DataLocator;

namespace reg {

    typedef void* Id;
    enum Type {MIDI, LOCATOR, NONE, FIRST};
};

using namespace reg;
class Register
{

    friend class Engine;
public:
    Register();
    ~Register();

    const Id insert(const AData& data, Gl::TicksTime time, const AData* parent);

    void read (const Id id,  AData* data_out, Gl::TicksTime time_out, AData* parent_out, Type type_out) const;
    const QList <Id> & children (const Id id) const;

    bool remove(Id id);
    bool move(Id id, Gl::TicksTime);

    void setType(Id id, const DataMidi & data) const {
        if (!id) { static_cast <Record*> (id)->type = MIDI;}
    }

    void setType(Id id, const DataLocator & data) const {
        if (!id) { static_cast <Record*> (id)->type = LOCATOR;}
    }

protected:
    struct Record {
        Record();
        ~Record();
        AData* data;
        Gl::TicksTime time;
        Type type;
        QList<Record*> children;
        Record* parent;
        Record* prev;
        Record* next;
    };

    Record * head_;
    QMutex mutex_;
private:
};

#endif // REGISTER_H
